import { LocalStorage, Notify } from 'quasar';

let SysPath: string;
let tempPath: string;
/**
 *   设置系统文件保存路径
 * @author qianl
 * @date 2023/8/7 16:21
 * @return
 * @param path
 */
export const setSysPath = (path: string) => {
  SysPath = path;
  LocalStorage.set('SysPath', SysPath);
};
/**
 *   获取项目目录
 * @author qianl
 * @date 2023/8/7 16:22
 * @return
 */
export const getSysPath = () => {
  return window.ipcCom.getDirname();
};
/**
 *   获取文档目录
 * @author qianl
 * @date 2023/8/7 16:22
 * @return
 */
export const getDocDirname = () => {
  return window.ipcCom.getDocDirname();
};
/**
 *   获取文件保存路径
 * @author qianl
 * @date 2023/8/7 16:22
 * @return
 */
export const getFileDirname = (src: string) => {
  return window.ipcCom.getFileDirname(src);
};
/**
 *   获取文件保存路径
 * @author qianl
 * @date 2023/8/7 16:22
 * @return
 */
export const getFilePathTree = (path: string): Promise<string[]> => {
  return new Promise((resolve) => {
    resolve(window.ipcCom.getFilePathTree(path));
  });
};
/**
 *   获取文件保存路径
 * @author qianl
 * @date 2023/8/7 16:22
 * @return
 */
export const isFile = (path: string): Promise<boolean> => {
  return new Promise((resolve) => {
    resolve(window.ipcCom.isFile(path));
  });
};
/**
 *   获取文件保存路径
 * @author qianl
 * @date 2023/8/7 16:22
 * @return
 */

export const extname = (path: string): string => {
  return window.ipcCom.extname(path);
};
/**
 *   获取系统文件
 * @author qianl
 * @date 2023/8/7 16:22
 * @return  文件内容
 * @param fileName 文件名
 * @param init 未到文件创建文件返回空
 */
export const getSysFile = (
  fileName: string,
  init: boolean
): Promise<string> => {
  return new Promise((resolve) => {
    resolve(
      LocalStorage.getItem('SysPath') ||
        window.ipcCom.getFileText(pathJoin(SysPath, fileName), init)
    );
  });
};
/**
 *   保存系统文件
 * @author qianl
 * @date 2023/8/7 16:24
 * @return
 * @param fileName 文件名
 * @param data 文件内容
 */
export const saveSysFile = (
  fileName: string,
  data: string
): Promise<string> => {
  return new Promise((resolve) => {
    resolve(
      window.ipcCom.saveFileText(
        pathJoin(
          SysPath || pathJoin(window.ipcCom.getDirname(), '..', 'sys'),
          fileName
        ),
        data
      )
    );
  });
};
/**
 *   有更新
 * @author qianl
 * @date 2023/8/7 16:03
 * @return
 */
export const hasUpdate = (): Promise<boolean> => {
  return new Promise((resolve) => {
    Notify.create({
      type: 'info',
      message: '更新成功!',
    });
    resolve(true);
  });
};
export const pathJoin = (...paths: string[]): string => {
  return window.ipcCom.pathJoin(paths);
};

/**
 *   获取临时文件
 * @author qianl
 * @date 2023/8/7 16:22
 * @return  文件内容
 * @param fileName 文件名
 * @param init 未到文件创建文件返回空
 */
export const getTempFile = (
  fileName: string,
  init: boolean
): Promise<string> => {
  return new Promise((resolve) => {
    resolve(
      window.ipcCom.getFileText(
        pathJoin(
          tempPath || pathJoin(window.ipcCom.getDirname(), '..', 'temp'),
          fileName
        ),
        init
      )
    );
  });
};
/**
 *   保存临时文件
 * @author qianl
 * @date 2023/8/7 16:24
 * @return
 * @param fileName 文件名
 * @param data 文件内容
 */
export const saveTempFile = (
  fileName: string,
  data: string
): Promise<string> => {
  return new Promise((resolve) => {
    resolve(
      window.ipcCom.saveFileText(
        pathJoin(
          tempPath || pathJoin(window.ipcCom.getDirname(), '..', 'temp'),
          fileName
        ),
        data
      )
    );
  });
};
export const explorerFolder = (src: string) => {
  window.ipcCom.explorerFolder(src);
};

export const notion = (title: string, body: string, path:string) => {
  window.ipcCom.notion(title, body, path);
};
